﻿using System;
using System.Web.UI;
using Module;
using Module.QuanTriHeThong.DanhMuc;

namespace HospitalManager.Site
{
    public partial class DangNhap : Page
    {
        private readonly DBInteractionBase _dbInteractionBase = new DBInteractionBase();

        private readonly DMTaiKhoan _dmTaiKhoan = new DMTaiKhoan();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            if (Session["username"] != null)
            {
                Response.RedirectToRoute("Index");
            }
        }

        protected void btnDangNhap_Click(object sender, EventArgs e)
        {
            bool statuslogin;
            _dmTaiKhoan.KiemTraTaiKhoan(txtTaiKhoan.Text.Trim(), _dbInteractionBase.Encrypt(txtMatKhau.Text.Trim()), true, out statuslogin);
            if (statuslogin){
                var dataTaiKhoan = _dmTaiKhoan.SelectById(txtTaiKhoan.Text.Trim());
                Session["username"] = txtTaiKhoan.Text.Trim();
                Session["makhoa"] = dataTaiKhoan.Rows[0]["MaKhoa"];
                Session["superadmin"] = Convert.ToBoolean(dataTaiKhoan.Rows[0]["SuperAdmin"].ToString())
                    ? "true"
                    : "false";
                Response.RedirectToRoute("Index");
            }
            else
            {
                Response.RedirectToRoute("DangNhap");
            }
        }
    }
}